import { InfraFileModel } from '@/models/infraFile'
import { getInfraFilePage, InfraFileVO, removeInfraFile } from '@/services/InfraFileService'
import { useAccess, useModel } from '@@/exports'
import { ProColumns, ProTable } from '@ant-design/pro-components'
import { Button, Popconfirm, Space } from 'antd'

export interface InfraFileProps {
  tableRef?: any
}

const InfraFileTable = (props: InfraFileProps) => {
  const access = useAccess()
  let { actions: notifyActions } = InfraFileModel

  let { getDictData, getDictEnum } = useModel('dict')

  /**
   * 列定义
   */
  const columns: ProColumns<InfraFileVO>[] = [
    {
      title: '文件编号',
      dataIndex: 'id',
      width: 120,
      hideInTable: true,
      hideInSearch: true,
    },
    {
      title: '配置编号',
      dataIndex: 'configId',
      width: 120,
      hideInSearch: true,
    },
    {
      title: '文件名',
      dataIndex: 'name',
      width: 240,
      ellipsis: true,
      hideInSearch: true,
    },
    {
      title: '文件路径',
      dataIndex: 'path',
      width: 240,
      ellipsis: true,
      hideInSearch: true,
    },
    {
      title: '文件 URL',
      dataIndex: 'url',
      width: 120,
      valueType: 'image',
      hideInSearch: true,
    },
    {
      title: '文件类型',
      dataIndex: 'type',
      width: 120,
      hideInSearch: true,
    },
    {
      title: '文件大小',
      dataIndex: 'size',
      width: 120,
      hideInSearch: true,
    },
    {
      title: '创建人',
      dataIndex: 'creator',
      width: 120,
      hideInSearch: true,
    },
    {
      title: '创建时间',
      dataIndex: 'createTime',
      width: 200,
      hideInSearch: true,
    },
    {
      title: '更新人',
      dataIndex: 'updater',
      width: 120,
      hideInSearch: true,
    },
    {
      title: '更新时间',
      dataIndex: 'updateTime',
      width: 200,
      hideInSearch: true,
    },
    {
      title: '是否删除',
      dataIndex: 'isDeleted',
      width: 240,
      hideInSearch: true,
    },
    {
      title: '操作',
      render: (_: any, record: InfraFileVO) => [
        <Space key="operate">
          {access.hasInfraFileUpdatePerm && (
            <Button type="link" size="small" onClick={() => notifyActions.openUpdateModal(true, record)}>
              编辑
            </Button>
          )}
          {access.hasInfraFileUpdatePerm && (
            <Popconfirm
              title="确定要删除吗"
              onConfirm={() => {
                removeInfraFile(record.id).then(() => props.tableRef?.current?.resetFields())
              }}
              okText="确认"
              cancelText="取消"
            >
              <Button type="link" danger size="small">
                删除
              </Button>
            </Popconfirm>
          )}
        </Space>,
      ],
      fixed: 'right',
      width: 150,
      hideInSearch: true,
    },
  ]

  // 工具栏
  let actions = [
    access.hasInfraFileSavePerm && (
      <Button key="create" type="primary" onClick={() => notifyActions.openUpdateModal(true, undefined)}>
        添加
      </Button>
    ),
  ]

  return (
    <>
      <ProTable
        actionRef={props.tableRef}
        columns={columns}
        request={getInfraFilePage}
        toolbar={{
          actions: actions,
        }}
        scroll={{ x: 500 }}
        sticky={{ offsetHeader: 55, offsetScroll: 5 }}
      ></ProTable>
    </>
  )
}

export default InfraFileTable
